﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Domain.Models;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Domain.DAL
{
    public class RepairingTicketHistoryDAO
    {
        #region Private Fields

        private Database db;

        #endregion

        #region Constructors

        public RepairingTicketHistoryDAO(Database theDatabase)
        {
            this.db = theDatabase;
        }

        #endregion

        #region Public Methods

        public RepairingTicketHistory Get(long id)
        {
            RepairingTicketHistory theRepairingTicketHistory = new RepairingTicketHistory();
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RepairingTicketHistories_Get", id);
                if(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.ID = Convert.ToInt64(ds.Tables[0].Rows[0]["ID"]);
                    }
                    if (ds.Tables[0].Rows[0]["TicketID"] != null && ds.Tables[0].Rows[0]["TicketID"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.TicketID = Convert.ToInt64(ds.Tables[0].Rows[0]["TicketID"]);
                    }
                    if (ds.Tables[0].Rows[0]["Repairer"] != null && ds.Tables[0].Rows[0]["Repairer"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.Repairer = Convert.ToInt32(ds.Tables[0].Rows[0]["Repairer"]);
                    }
                    if (ds.Tables[0].Rows[0]["Status"] != null && ds.Tables[0].Rows[0]["Status"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.Status = Convert.ToInt32(ds.Tables[0].Rows[0]["Status"]);
                    }
                    if (ds.Tables[0].Rows[0]["RepairingType"] != null && ds.Tables[0].Rows[0]["RepairingType"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.RepairingType = Convert.ToInt32(ds.Tables[0].Rows[0]["RepairingType"]);
                    }
                    if(ds.Tables[0].Rows[0]["CreatedDate"] != null && ds.Tables[0].Rows[0]["CreatedDate"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.CreatedDate = ds.Tables[0].Rows[0]["CreatedDate"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["NextRepairingDate"] != null && ds.Tables[0].Rows[0]["NextRepairingDate"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.NextRepairingDate = ds.Tables[0].Rows[0]["NextRepairingDate"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Description"] != null && ds.Tables[0].Rows[0]["Description"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["Cost"] != null && ds.Tables[0].Rows[0]["Cost"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.Cost = Convert.ToDecimal(ds.Tables[0].Rows[0]["Cost"]);
                    }
                    if (ds.Tables[0].Rows[0]["Note"] != null && ds.Tables[0].Rows[0]["Note"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.Note = ds.Tables[0].Rows[0]["Note"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["IsLastest"] != null && ds.Tables[0].Rows[0]["IsLastest"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.IsLastest = ds.Tables[0].Rows[0]["IsLastest"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["IsDeleted"] != null && ds.Tables[0].Rows[0]["IsDeleted"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.IsDeleted = ds.Tables[0].Rows[0]["IsDeleted"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["IsValidWarranty"] != null && ds.Tables[0].Rows[0]["IsValidWarranty"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.IsValidWarranty = ds.Tables[0].Rows[0]["IsValidWarranty"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["WarrantyNote"] != null && ds.Tables[0].Rows[0]["WarrantyNote"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.WarrantyNote = ds.Tables[0].Rows[0]["WarrantyNote"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["WarrantyConfirmer"] != null && ds.Tables[0].Rows[0]["WarrantyConfirmer"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.WarrantyConfirmer = Convert.ToInt32(ds.Tables[0].Rows[0]["WarrantyConfirmer"].ToString());
                    }
                    if (ds.Tables[0].Rows[0]["WarrantyConfirmDate"] != null && ds.Tables[0].Rows[0]["WarrantyConfirmDate"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.WarrantyConfirmDate = ds.Tables[0].Rows[0]["WarrantyConfirmDate"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["RepairQualityID"] != null && ds.Tables[0].Rows[0]["RepairQualityID"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.RepairQualityID = Convert.ToInt32(ds.Tables[0].Rows[0]["RepairQualityID"].ToString());
                    }
                    if (ds.Tables[0].Rows[0]["RepairQualityNote"] != null && ds.Tables[0].Rows[0]["RepairQualityNote"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.RepairQualityNote = ds.Tables[0].Rows[0]["RepairQualityNote"].ToString();
                    }
                    if (ds.Tables[0].Rows[0]["RepairQualityConfirmer"] != null && ds.Tables[0].Rows[0]["RepairQualityConfirmer"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.RepairQualityConfirmer = Convert.ToInt32(ds.Tables[0].Rows[0]["RepairQualityConfirmer"].ToString());
                    }
                    if (ds.Tables[0].Rows[0]["RepairQualityConfirmDate"] != null && ds.Tables[0].Rows[0]["RepairQualityConfirmDate"] != DBNull.Value)
                    {
                        theRepairingTicketHistory.RepairQualityConfirmDate = ds.Tables[0].Rows[0]["RepairQualityConfirmDate"].ToString();
                    }
                }
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return theRepairingTicketHistory;
        }

        #endregion

        #region Public Static Methods

        public static RepairingTicketHistoryDAO GetInstance()
        {
            return EnterpriseLibraryContainer.Current.GetInstance<RepairingTicketHistoryDAO>();
        }

        #endregion
    }
}
